<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="hAdd(1,'0')">添加权限</el-button>
        </div>
        <el-table :data="list" border row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button v-if="scope.row.type===1" type="text" @click="hAdd(2,scope.row.id)">添加</el-button>
              <el-button type="text" @click="hEdit(scope.row)">编辑</el-button>
              <el-button type="text" @click="hDel(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <el-dialog
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        :title="isEdit?'编辑':'添加'"
        @close="doClose"
      >
        <!-- 表单内容 -->
        <el-form ref="formData" :rules="rules" label-width="100px" :model="formData">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用" prop="enVisible">
            <el-switch
              v-model="formData.enVisible"
              active-text="启用"
              active-value="1"
              inactive-text="不启用"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <div style="text-align: right;">
            <el-button @click="showDialog = false">取消</el-button>
            <el-button type="primary" @click="hSubmit">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { toTree } from '@/utils'
import { getPermissionList, addPermission, delPermission, updatePermission } from '@/api/permisson'
export default {
  data() {
    // 权限名称判断
    const validateName = (rule, value, callback) => {
      if (this.isEdit || this.formData.type === 1) {
        const aaa = this.valList.filter(item => item.pid === this.formData.pid).filter(item => item.id !== this.formData.id).some(item => item.name.trim() === value.trim())
        aaa ? callback(new Error('用户名重复')) : callback()
      } else {
        const bbb = this.valList.filter(item => item.pid === this.formData.pid).some(item => item.name === value)
        bbb ? callback(new Error('用户名重复')) : callback()
      }
    }
    // 权限表示判断
    const validateCode = (rule, value, callback) => {
      if (!this.isEdit) {
        const ccc = this.valList.some(item => item.code === value.trim())
        ccc ? callback(new Error('权限标识重复')) : callback()
      } else {
        const ddd = this.valList.filter(item => item.id !== this.formData.id).some(item => item.code === value.trim())
        ddd ? callback(new Error('用户名重复')) : callback()
      }
    }
    return {
      list: [],
      valList: [],
      showDialog: false, // 是否显示弹层
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      },
      isEdit: false,
      rules: {
        name: [
          { required: true, message: '请输入权限名称', trigger: 'blur' },
          { validator: validateName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入权限标识', trigger: 'blur' },
          { validator: validateCode, trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入权限描述', trigger: 'blur' }
        ],
        enVisible: [
          { required: true, message: '请输入权限启用', trigger: 'blur' }
        ]

      }
    }
  },
  created() {
    this.getPermission()
  },
  methods: {
    // 获取权限信息
    async getPermission() {
      try {
        const res = await getPermissionList()
        this.valList = res.data
        console.log(this.valList)

        this.list = toTree(res.data)
      } catch (err) {
        console.log('权限获取失败', err)
      }
    },
    // 点击按钮添加信息
    hAdd(type, id) {
      this.showDialog = true
      this.isEdit = false
      // 记录当前添加的关键信息
      this.formData.type = type
      this.formData.pid = id
    },
    // 点击按钮编辑信息
    hEdit(row) {
      this.showDialog = true
      this.isEdit = true
      // 1 表单数据回填
      this.formData = { ...row }
    },
    // 表单确定兜底
    hSubmit() {
      this.$refs.formData.validate(valid => {
        if (!valid) return
        this.isEdit ? this.doEdit() : this.doAdd()
      })
    },
    // 编辑数据
    async doEdit() {
      try {
        // 1 发送请求
        await updatePermission(this.formData)
        // 2 提示成功
        this.$message.success('修改成功')
        // 3 渲染页面
        this.getPermission()
        // 4 关闭对话框
        this.showDialog = false
        // 保存数据
      } catch (err) {
        console.log('编辑失败', err)
      }
    },
    // 添加数据
    async doAdd() {
      try {
        // 1 发送请求
        await addPermission(this.formData)
        // 2 提示成功
        this.$message.success('添加成功')
        // 3 渲染页面
        this.getPermission()
        // 4 关闭对话框
        this.showDialog = false
      } catch (err) {
        this.$message.error('添加失败')
      }
    },
    // 删除数据
    async hDel(id) {
      try {
        // 1 发送请求
        await delPermission(id)
        // 2 通知成功
        this.$message.success('删除成功')
        // 3 渲染页面
        this.getPermission()
        // 保存数据
      } catch (err) {
        console.log('delPermission', err)
      }
    },
    // 关闭对话框回调
    doClose() {
    // 重置表单
      this.formData = {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      }
      // 重置验证消息
      this.$refs.formData.resetFields()
    }
  }
}
</script>
